﻿using System.Web;
using CustomerAssistant.Core;
using CustomerAssistant.DAL;
using Itour.Models;
using System.IO;

namespace CustomerAssistantWebServer
{
    public class DownloadClientHandle : IClientHandle
    {
        public IClientHandle NextNode
        {
            get;
            set;
        }

        public string RequestKey
        {
            get { return "download"; }
        }

        public void Response(HttpRequest Request, Stream OutputStream)
        {
            if (string.Compare(Request[Util.REQUEST_KEY], RequestKey, true) == 0)
            {
                using (FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/CustomerAssistantSetup.msi"), FileMode.Open))
                {
                    using (BinaryWriter sw = new BinaryWriter(OutputStream))
                    {
                        byte[] bs = new byte[1024];
                        for (int size = fs.Read(bs, 0, bs.Length); size > 0; size = fs.Read(bs, 0, bs.Length))
                        {
                            sw.Write(bs, 0, size);
                        }
                        sw.Flush();
                    }
                }
            }
            else
            {
                if (NextNode != null)
                    NextNode.Response(Request, OutputStream);
            }
        }
    }
}